package com.chengwei.ch.busdemo.utils;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.chengwei.ch.busdemo.APP;
import com.chengwei.ch.busdemo.view.ClearEditText;

import static android.content.Context.INPUT_METHOD_SERVICE;

public class AppUtil {
    public static String TAG = "busDemo";
    /**
     * 获取屏幕宽高
     *
     * @param context 上下文
     * @return [宽px, 高px]
     */
    public int[] getScreenWidthHeight(Context context) {
        WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        assert manager != null;
        manager.getDefaultDisplay().getMetrics(dm);

        //9.0+屏幕高需要加状态栏高度
        return Build.VERSION.SDK_INT >= 28 ? new int[]{dm.widthPixels, dm.heightPixels + this.getStatusBarHeight(context)}
                : new int[]{dm.widthPixels, dm.heightPixels};
    }

    /**
     * 获取状态栏高度
     *
     * @param context 上下文
     * @return px
     */
    public int getStatusBarHeight(Context context) {
        int statusBarHeight = 0;
        Resources res = context.getResources();
        int resourceId = res.getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            statusBarHeight = res.getDimensionPixelSize(resourceId);
        }
        return statusBarHeight;
    }

    /**
     * 显示键盘
     *
     * @param et 输入焦点
     */
    public void showInput(Activity activity, EditText et) {
        et.requestFocus();
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(INPUT_METHOD_SERVICE);
        imm.showSoftInput(et, InputMethodManager.SHOW_IMPLICIT);
    }

    /**
     * 隐藏键盘
     */
    public void hideInput(Activity activity) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(INPUT_METHOD_SERVICE);
        View v = activity.getWindow().peekDecorView();
        if (null != v) {
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
        }
    }
}
